package study.eggs.utils;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

public class DateUtils {

    /* 标准格式 */
    private static String STD_YMD_HMS_S = "yyyy-MM-dd HH:mm:ss.SSS";
    private static String STD_YMD_HMS = "yyyy-MM-dd HH:mm:ss";
    private static String STD_YMD = "yyyy-MM-dd";
    private static String STD_YM = "yyyy-MM";
    private static String STD_Y = "yyyy";
    private static String STD_MM = "MM";
    private static String STD_D = "dd";
    private static String STD_HMS = "HH:mm:ss";
    /* 短格式 */
    private static String SHORT_YMD_HMS_S = "yyyyMMddHHmmssSSS";
    private static String SHORT_YMD_HMS = "yyyyMMddHHmmss";
    private static String SHORT_YMD = "yyyyMMdd";
    private static String SHORT_HMS = "HHmmss";

    /**
     * ==============================
     * 获取当前时间
     * ==============================
     */
    public static Date now() {
        return new Date();
    }

    /**
     * ==============================
     * 获取当前时间戳
     * ==============================
     */
    public static long timestamp() {
        long ts = System.currentTimeMillis();
        return Math.round(ts / 1000);
    }

    /**
     * ==============================
     * Date转字符串
     * ==============================
     *
     * @param date -Date
     */
    public static String toStdStr(Date date) {
        return toStr(date, STD_YMD_HMS);
    }

    public static String toStdYMStr(Date date) {
        return toStr(date, STD_YM);
    }

    /**
     * @param date   -Date
     * @param format -String
     */
    private static String toStr(Date date, String format) {
        if (date == null) {
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(format);
    }

    /**
     * ==============================
     * 字符串转Date
     * ==============================
     *
     * @param dateStr -String
     */
    public static Date parseStdDate(String dateStr) {
        return parseDate(dateStr, STD_YMD_HMS);
    }

    public static Date parseStdYMDate(String dateStr) {
        return parseDate(dateStr, STD_YM);
    }


    /**
     * @param dateStr -String
     * @param format  -String
     */
    public static Date parseDate(String dateStr, String format) {
        if (dateStr == null) {
            return null;
        }
        DateTimeFormatter fmt = DateTimeFormat.forPattern(format);
        DateTime dateTime = fmt.parseDateTime(dateStr);
        return dateTime.toDate();
    }

    /**
     * ==============================
     * 获取年月日
     * ==============================
     */
    public static int getYear(Date date) {
        DateTime dtime = new DateTime(date);
        return dtime.getYear();
    }

    public static int getMonth(Date date) {
        DateTime dtime = new DateTime(date);
        return dtime.getMonthOfYear();
    }

    public static int getDay(Date date) {
        DateTime dtime = new DateTime(date);
        return dtime.getDayOfMonth();
    }

    /**
     * ==============================
     * 计算
     * ==============================
     *
     * @param date   -Date
     * @param months -int
     */
    public static Date plusMonths(Date date, int months) {
        DateTime dtime = new DateTime(date);
        DateTime dt = dtime.plusMonths(months);
        return dt.toDate();
    }

    public static Date plusDays(Date date, int days) {
        DateTime dtime = new DateTime(date);
        DateTime dt = dtime.plusDays(days);
        return dt.toDate();
    }

    public static Date plusHours(Date date, int hours) {
        DateTime dtime = new DateTime(date);
        DateTime dt = dtime.plusHours(hours);
        return dt.toDate();
    }

    public static Date plusMinutes(Date date, int minutes) {
        DateTime dtime = new DateTime(date);
        DateTime dt = dtime.plusMinutes(minutes);
        return dt.toDate();
    }

    /**
     * ==============================
     * 求差
     * ==============================
     *
     * @param start -Date
     * @param end   -Date
     */
    public static int diffMinutes(Date start, Date end) {
        Interval interval = new Interval(start.getTime(), end.getTime());
        Period period = interval.toPeriod(PeriodType.minutes());
        return period.getMinutes();
    }

    public static int diffHours(Date start, Date end) {
        Interval interval = new Interval(start.getTime(), end.getTime());
        Period period = interval.toPeriod(PeriodType.hours());
        return period.getHours();
    }

    public static int diffDays(Date start, Date end) {
        Interval interval = new Interval(start.getTime(), end.getTime());
        Period period = interval.toPeriod(PeriodType.days());
        return period.getDays();
    }

    /**
     * ==============================
     * 比较
     * ==============================
     *
     * @param d1 -Date
     * @param d2 -Date
     */
    public static boolean isBefore(Date d1, Date d2) {
        DateTime dt1 = new DateTime(d1);
        DateTime dt2 = new DateTime(d2);
        return dt1.isBefore(dt2);
    }

    public static boolean isBeforeNow(Date date) {
        DateTime dt1 = new DateTime(date);
        return dt1.isBeforeNow();
    }
}
